package com.css.fxfzcpxx.modules.serviceCase.controller;


import com.alibaba.fastjson.JSONObject;
import com.css.fxfzcpxx.base.log.Log;
import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.modules.informationDelivery.entity.InfDelivery;
import com.css.fxfzcpxx.modules.serviceCase.entity.ServiceCase;
import com.css.fxfzcpxx.modules.serviceCase.param.ClaCasParams;
import com.css.fxfzcpxx.modules.serviceCase.param.QueParams;
import com.css.fxfzcpxx.modules.serviceCase.service.ServiceCaseService;
import com.css.fxfzcpxx.modules.serviceCase.vo.ListVo;
import com.css.fxfzcpxx.modules.serviceCase.vo.ServiceCaseVo;
import com.css.fxfzcpxx.modules.websocket.WebSocketManager;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 徐治宇
 * @description 服务案例
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/serviceCase")
@Slf4j
public class ServiceCaseController {
    @Resource
    ServiceCaseService serviceCaseService;

    private final WebSocketManager webSocketManager;

    @Autowired
    public ServiceCaseController(WebSocketManager webSocketManager) {
        this.webSocketManager = webSocketManager;
    }
    /***
     * 分页查询
     * @param queParams
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(QueParams queParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=serviceCaseService.getPage(queParams,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 保存
     *
     * @param serviceCase
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody ServiceCase serviceCase) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            serviceCaseService.save(serviceCase);
            jsonObject.put("message", "保存成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 撤回/发布
     *
     * @param queParams
     * @return
     */
    @PostMapping("/recallOrPublish")
    public RestResponse recallOrPublish(@RequestBody QueParams queParams) {
        RestResponse response = null;
        try {
            serviceCaseService.recallOrPublish(queParams);
            if("1".equals(queParams.getReleaseStatus())){
                webSocketManager.broadcastMessage("服务案例发布啦","serviceCase");
            }else{
                webSocketManager.broadcastMessage("服务案例撤回啦","serviceCase");
            }
        } catch (Exception e) {
            String errorMessage="";
            if("1".equals(queParams.getReleaseStatus())){
                errorMessage = "发布失败";
            }else{
                errorMessage = "撤回失败";
            }
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids){
        RestResponse response = null;
        try{
            serviceCaseService.delete(ids);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(@RequestParam("id") String id) {
        RestResponse response = null;
        try {
            ServiceCaseVo infAchievement=serviceCaseService.findById(id);
            response = RestResponse.succeed(infAchievement);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody ServiceCase serviceCase) {
        RestResponse response = null;
        try {
            serviceCaseService.update(serviceCase);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 目录下载
     * @param
     * @return
     */
    @PostMapping("/catalogDownload/{ids}")
    public void catalogDownload(HttpServletResponse response, @PathVariable("ids") String ids) {
        RestResponse restResponse = null;
        try {
            serviceCaseService.catalogDownload(response, ids);
            //restResponse = RestResponse.succeed("下载成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            //restResponse = RestResponse.fail(errormessage);
        }
        //return restResponse;
    }


    /***
     * 获取迁移数据
     * @param
     * @return
     */
    @GetMapping("/getMoveData")
    public RestResponse getMoveData(QueParams params) {
        RestResponse response = null;
        try {
            List<ServiceCase> dataList = serviceCaseService.getMoveData(params);
            response = RestResponse.succeed(dataList);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 资料分类修改
     * @param
     * @return
     */
    @PostMapping("/meansTypeUpdate")
    public RestResponse meansTypeUpdate(@RequestBody ListVo listvo){
        RestResponse response = null;
        try{
            serviceCaseService.meansTypeUpdate(listvo);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 公众端分页查询
     * @param claCasParams
     * @return
     */
    @GetMapping("/claCasPage")
    @Log(value="服务案例",type = "检索")
    public RestResponse getClaCasPage(ClaCasParams claCasParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=serviceCaseService.getClaCasPage(claCasParams,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 公众端首页最新案例
     * @return
     */
    @GetMapping("/getLatestCase")
    public RestResponse getLatestCase(){
        RestResponse response = null;
        try{
            List<ServiceCase> list=serviceCaseService.getLatestCase();
            response= RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 获取轮播图列表
     * @return
     */
    @GetMapping("/getRotationChartData")
    public RestResponse getRotationChartData(){
        RestResponse response = null;
        try{
            List<ServiceCaseVo> list=serviceCaseService.getRotationChartData();
            response= RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 设为首页轮播
     * @return
     */
    @PostMapping("/setRotationChart/{ids}")
    public RestResponse setRotationChart(@PathVariable("ids") List<String> ids){
        RestResponse response = null;
        try{
            serviceCaseService.setRotationChart(ids);
            webSocketManager.broadcastMessage("设为轮播图","rotationChart");
            response= RestResponse.succeed();
            response.setMessage("设置成功");
        }catch (Exception e){
            String errorMessage = "设置失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 取消首页轮播
     * @return
     */
    @PostMapping("/cancelRotationChart/{ids}")
    public RestResponse setRotationChartData(@PathVariable("ids") List<String> ids){
        RestResponse response = null;
        try{
            serviceCaseService.cancelRotationChart(ids);
            webSocketManager.broadcastMessage("取消轮播图","rotationChart");
            response= RestResponse.succeed();
            response.setMessage("取消成功");
        }catch (Exception e){
            String errorMessage = "取消失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


}
